/* Appendix Table 1 IN COSAR, GRIECO AND TINTELNOT (2014) 
Inputs: 
- gravdata_cepii.dta, gravity data from CEPII
- aggregateWindTradeData.dta, trade flows for "wind-powered generating sets� with the HS 2007 code 8502.31, downloaded from WITS database (http://wits.worldbank.org/wits/)
Outputs: 
- tableA1.txt , Appendix table 1
*/

clear
global path "C:\Users\Felix Tintelnot\Dropbox\WindBorder\RestatCGTReplication"

// Load and prepare data 
use "$path\Data\original_files\aggregateWindTradeData.dta"
drop nomenclature reportername partnername
keep if tradeflowcode==6 // keep exports ==6, imports==5*
drop tradeflowcode tradeflowname

drop if partneriso3=="WLD" // world
drop if reporteriso3=="WLD" 
drop if partneriso3=="EUN" // european union
drop if reporteriso3=="EUN"

sort year productcode reporteriso3 partneriso3
order year productcode reporteriso3 partneriso3

collapse (sum) tradevaluein1000usd, by (reporteriso3 partneriso3 productcode) // aggregate over years , 
sort productcode reporteriso3 partneriso3 

rename reporteriso3 iso3_o // rename reporter and partner to merge trade data with gravity variables
label variable iso3_o "origin"
rename partneriso3 iso3_d
label variable iso3_d "destination"

replace iso3_d="SER" if iso3_d=="YUG"
replace iso3_o="SER" if iso3_o=="YUG"

save temp,replace

/* Merge with wind turbine trade data with gravity data of Mayer et al from cepii */
use "$path\Data\original_files\gravdata_cepii.dta",clear
merge m:m iso3_o iso3_d using temp.dta
drop if iso3_o==iso3_d

order iso3_o iso3_d tradevaluein1000usd _merge
sort iso3_o iso3_d tradevaluein1000usd 

keep if _merge==3

// Drop zeros 
drop if tradevaluein1000usd==. | tradevaluein1000usd==0  

/* log of variables */
replace tradevaluein1000usd=log(tradevaluein1000usd)
replace distw=log(distw)
replace gdp_o=log(gdp_o) 
replace gdp_d=log(gdp_d)
replace pop_o=log(pop_o)
replace pop_d=log(pop_d)
replace gdpcap_d=log(gdpcap_d)
replace gdpcap_o=log(gdpcap_o)

// estimation
// product and destination gdp and population
qui tabulate productcode, gen(productdum)
qui tabulate iso3_d, gen(destdum)
qui tabulate iso3_o, gen(origindum)

eststo: qui areg tradevaluein1000usd origindum* contig comlang_off comlang_ethno comcur acp_to_eu eu_to_acp col_to colony tdiff rta distw,  absorb(iso3_d) vce(robust)
estimates store generatingsets
estadd local component "Generating sets", replace
estadd local fixedeffects "Yes" , replace

esttab generatingsets, compress width(1\hsize)label keep(distw contig) varlabels(distw "Distance" contig "Common border") se star(* 0.10 ** 0.05 *** 0.01) s(fixedeffects N r2, label("Country fixed effects" "N" "R^2")) nonumbers nodepvars nomtitles title("Gravity in Wind Turbine Trade") nonotes
esttab generatingsets using "$path\SummaryStatistics\tableA1.txt", replace compress width(1\hsize)label keep(distw contig) varlabels(distw "Distance" contig "Common border") se star(* 0.10 ** 0.05 *** 0.01) s(fixedeffects N r2, label("Country fixed effects" "N" "R^2")) nonumbers nodepvars nomtitles title("Gravity in Wind Turbine Trade") nonotes

erase temp.dta
